﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ccit.ClassLibrary;
using Ccit.Utils;

namespace Ccit.SysPlat.Purview
{
    /// <summary>
    /// Description: 系统平台权限菜单 -> 权限管理树页面
    /// Author: tim 2010-03-04
    /// </summary>
    public partial class sys_purview_tree : Ccit.Utils.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                _hidOrganId.Value = (null != Request.QueryString["organId"]) ? Request.QueryString["organId"].ToString() : "1";
                initializePage();
            }
        }

        private void initializePage()
        {
            createTree();
            trvRole.ExpandLevel = 1;
        }

        private void createTree()
        {
            /* c创建根节点 */
            Microsoft.Web.UI.WebControls.TreeNode oRoot = new Microsoft.Web.UI.WebControls.TreeNode();
            createRootNode(ref oRoot);
            trvRole.Nodes.Add(oRoot);
            createSubNodes(ref oRoot);
        }

        private void createRootNode(ref Microsoft.Web.UI.WebControls.TreeNode oRoot)
        {
            oRoot.Text = "角色列表";
            oRoot.ImageUrl = "../images/toolbar/role_root.gif";
            oRoot.NavigateUrl = "";
            oRoot.Target = "";
        }

        private void createSubNodes(ref Microsoft.Web.UI.WebControls.TreeNode oRoot)
        {
            string organId = _hidOrganId.Value;
            MsLoginUser loginUser = HttpContext.Current.Session["LogUserInfo"] as MsLoginUser;
            eUserType enUserType = loginUser.userType;
            eTrueFalse enSysRole = (enUserType == eUserType.ORDINARY) ? eTrueFalse.FALSE : eTrueFalse.TRUE;
            RoleBLL roleBLL = new RoleBLL();
            List<MsRole> lstRole = roleBLL.GetRoleList(organId, enSysRole, loginUser.userId);

            string roleId = string.Empty;
            string roleName = string.Empty;
            Microsoft.Web.UI.WebControls.TreeNode oTreeNode;
            foreach (MsRole eRole in lstRole)
            {
                roleId = eRole.roleId;
                roleName = eRole.roleName;
                eTrueFalse enCanEdit = eRole.canEdit;
                oTreeNode = createTreeNode(roleName, roleId, enCanEdit, organId);
                oRoot.Nodes.Add(oTreeNode);
            }

            lstRole = null;
            loginUser = null;
            roleBLL.Release();
            roleBLL = null;
        }

        private Microsoft.Web.UI.WebControls.TreeNode createTreeNode(string nodeText, string roleId, eTrueFalse canEdit,string organId)
        {
            Microsoft.Web.UI.WebControls.TreeNode oNode = new Microsoft.Web.UI.WebControls.TreeNode();
            oNode.Text = nodeText;
            string sPageParam = "organId=" + organId + "&roleId=" + roleId + "&edit=" + canEdit.ToString("D");
            string encodeParam = base64encode(sPageParam);
            oNode.NavigateUrl = "./sys_purview_edit.aspx?" + encodeParam;
            oNode.ImageUrl = "../images/toolbar/role.gif";
            oNode.Target = "ifrmEdit";
            return oNode;
        }

        private string base64encode(string sPageParam)
        {
            string encodeParam = string.Empty;
            if (string.IsNullOrEmpty(sPageParam)) return encodeParam;

            encodeParam = Base64Lib.Encode(sPageParam);
            return encodeParam;
        }
    }
}
